package com.cn.yang.servlet;

import com.alibaba.fastjson.JSONObject;
import com.cn.yang.model.mainPage.HeadLinkModel;
import com.cn.yang.model.message.MessageModel;
import com.cn.yang.service.MainPageService;
import com.cn.yang.service.MessageService;
import com.cn.yang.service.impl.MainPageServiceImpl;
import com.cn.yang.service.impl.MessageServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/message")
public class MessageServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //获得headLink的List。
        MainPageService headLink = new MainPageServiceImpl();
        List<HeadLinkModel> list = headLink.selectHeadLink();
       
        //====================================================================================
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list",list);
        resp.getWriter().println(jsonObject);
    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //message的Insert。
        String name = req.getParameter("name");
        String email = req.getParameter("email");
        String content = req.getParameter("content");
        String isrecontent ="否";
        MessageModel model = new MessageModel();
        model.setName(name);
        model.setContent(content);
        model.setEmail(email);
        model.setIsrecontent(isrecontent);
    
        MessageService service = new MessageServiceImpl();
        int result = service.addMessage(model);
    
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
}
